from copy import deepcopy
from itertools import cycle
import random
from .tools import tool
from collections import OrderedDict
from loading import row, col


# HEIGHT=650
# WIDTH=520

# 添加了 nowash,wash_left_wash_down 等等 ，这是每次洗牌的位置排列
def notwash(w, number=2):
    return w


def wash_left(w, number=2):
    for i in range(number):
        for i in range(len(w)):
            for j in range(0, len(w[0]) - 1, 1):
                if w[i][j] == '*':
                    w[i][j], w[i][j + 1] = w[i][j + 1], w[i][j]
    return w


def wash_right(w, number=2):
    for i in range(number):
        for i in range(len(w)):
            for j in range(len(w[0]) - 1, 0, -1):
                if w[i][j] == '*':
                    w[i][j], w[i][j - 1] = w[i][j - 1], w[i][j]
    return w


def wash_up(w, number=2):
    for i in range(number):
        for i in range(len(w[0])):
            for j in range(0, len(w) - 1, 1):
                if w[j][i] == '*':
                    w[j][i], w[j + 1][i] = w[j + 1][i], w[j][i]
    return w


def wash_down(w, number=2):  # len(w)
    for q in range(number):
        for i in range(len(w[0])):
            for j in range(len(w) - 1, 0, -1):
                if w[j][i] == '*':
                    w[j][i], w[j - 1][i] = w[j - 1][i], w[j][i]
    return w


def wash_center(w, number=2):
    for qq in range(number):
        for i in range(col):
            for j in range(int(row / 2), 0, -1):
                if w[i][j] == '*':
                    w[i][j], w[i][j - 1] = w[i][j - 1], w[i][j]
            for j in range(int(row / 2), row - 1, 1):
                if w[i][j] == '*':
                    w[i][j], w[i][j + 1] = w[i][j + 1], w[i][j]
    return w


def is_same(w, x1, x2):
    return w[x1[0]][x1[1]] == w[x2[0]][x2[1]]


def fake_copy(total):
    t = []
    for i_ in total:
        i_ = deepcopy(i_)
        i_.append('*')
        i_.insert(0, '*')
        t.append(i_)
    c = len(t[0])
    t.append(list('*' * c))
    t.insert(0, list('*' * c))
    return t


def init_pos(row, col, width, height):  # 返回一个可以对比的列表
    pos_info = {}
    x = (width - row * 38) / 2
    y = (height - row * 38) / 2 + 38
    x_ = x + row * 38
    y_ = y + col * 38
    for i in range(col):
        for j in range(row):
            pos_info[(i, j)] = (x + (j) * 38, y + (i) * 38)
    return pos_info, (x, y), (x_, y_)


def get_real_post(tup, row, col, width, height):
    x = (width - row * 38) / 2
    y = (height - row * 38) / 2 + 38


def get_pos(tup, pos_info, sep):  # 填入当前位置和一个列表表对比，返回
    for i in pos_info:
        i_ = pos_info[i]
        if complie(tup, i_, sep):
            return i


def get_total(row, col, number):  # 返回图片的二维数组
    if col * row % 2 != 0:
        return
    tmp = [str(i) for i in range(number)]
    t = []
    cy = cycle(tmp)
    for i in range(col * row // 2):
        tmp_ = next(cy)
        t.append(tmp_)
        t.append(tmp_)
    total = []
    random.shuffle(t)
    for i in range(col):
        total.append(t[i * row:(i + 1) * row])
    return total


def wash(total):
    random.shuffle(total)
    for i in total:
        random.shuffle(i)
    return total


def complie(tup1, tup2, sep):
    if tup1[0] > tup2[0] and tup1[0] < tup2[0] + sep and tup1[1] > tup2[1] and tup1[1] < tup2[1] + sep:
        return True


# get_total(3,4,3)
# tup,pos_info=init_pos(10,10,WIDTH,HEIGHT)
# w=get_pos((350,300),pos_info)
# print(w)
def show_pic(screen, row, col, total, pic, start_pos):
    for i in range(col):
        for j in range(row):
            tmp = total[i][j]
            if tmp == '*':
                continue
            screen.blit(pic[tmp], start_pos[(i, j)])


def get_tip(total, total_):
    for j in range(len(total)):
        for i in range(len(total[0])):
            for j_ in range(len(total)):
                for i_ in range(len(total[0])):
                    if is_same(total, (j, i), (j_, i_)):
                        w = tool(total, i, j, i_, j_)
                        if w.main(total_) == True:
                            return [(j, i), (j_, i_)]


level_wash = {1: notwash, 2: wash_left, 3: wash_right, 4: wash_up, 5: wash_down, 6: wash_center}
# 利用上面的字典可以方便地挑选卡牌排序的位置
# for i in range(col):
# 	for j in range(row):
# 		tmp = total[i][j]
# 		if tmp == '*':
# 			continue
# 		screen.blit(pic[tmp] , start_pos[(i , j)])
